package cc.flyflow.core.listeners.impl;

import cc.flyflow.common.dto.process.ProcessInstanceNodeRecordParamDto;
import cc.flyflow.core.utils.BizHttpUtil;
import cc.flyflow.core.listeners.EventListenerStrategy;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.impl.FlowableActivityCancelledEventImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * 节点取消了
 * <p>
 * 活动取消事件监听器
 *
 * @author Huijun Zhao;Jyl
 * @date 2023-10-10 10:12
 */
@Slf4j
@Component
public class ActivityCanceledEventListener implements EventListenerStrategy, InitializingBean
{
    /**
     * 处理数据
     *
     * @param event
     * @return
     */
    @Override
    public void handle(FlowableEvent event)
    {
        //任务取消了
        FlowableActivityCancelledEventImpl activityCancelledEvent = (FlowableActivityCancelledEventImpl) event;

        String activityId = activityCancelledEvent.getActivityId();
        String activityName = activityCancelledEvent.getActivityName();
        String executionId = activityCancelledEvent.getExecutionId();
        String processInstanceId = activityCancelledEvent.getProcessInstanceId();
        DelegateExecution execution = activityCancelledEvent.getExecution();

        log.info("取消的节点：{}   {} {}  {}", activityId, activityName, processInstanceId, executionId);

        //节点取消了
        ProcessInstanceNodeRecordParamDto nodeRecordParamDto = new ProcessInstanceNodeRecordParamDto();
        nodeRecordParamDto.setProcessInstanceId(processInstanceId);
        nodeRecordParamDto.setExecutionId(executionId);
        nodeRecordParamDto.setNodeId(activityId);
        nodeRecordParamDto.setNodeName(activityName);

        nodeRecordParamDto.setTenantId(execution.getTenantId());
        BizHttpUtil.cancelNodeEvent(nodeRecordParamDto);
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        afterPropertiesSet(FlowableEngineEventType.ACTIVITY_CANCELLED.toString());
    }
}
